﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data.OleDb;

namespace DAO
{
    public class HoaDonDAO: AbtractDAO
    {
        public static List<HoaDonDTO> LaydsHoaDon()
        {
            OleDbConnection ketNoi = null;
            List<HoaDonDTO> dsHoaDon = new List<HoaDonDTO>();
            try
            {
                ketNoi = AbtractDAO.KetNoi();
                string chuoiLenh = "SELECT * FROM HoaDon";
                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);

                OleDbDataReader boLoc = lenh.ExecuteReader();

                while (boLoc.Read())
                {
                    HoaDonDTO HoaDon = new HoaDonDTO();
                    HoaDon.MaHoaDon = boLoc.GetInt32(0);
                    if (!boLoc.IsDBNull(1))
                    {
                        HoaDon.NgayLapHoaDon = boLoc.GetDateTime(1);
                    }
                    
                    if (!boLoc.IsDBNull(2))
                    {
                        HoaDon.TongTien = boLoc.GetInt32(2);
                    }
                    dsHoaDon.Add(HoaDon);
                }
            }
            catch (Exception ex)
            {
                dsHoaDon = new List<HoaDonDTO>();
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
            return dsHoaDon;
        }
        public static void ThemHoaDon(DateTime ngaylap,double tongtien,string maKH)
        {
            OleDbConnection ketNoi = null;
            try
            {
                ketNoi = AbtractDAO.KetNoi();
                string chuoiLenh = "INSERT INTO HOADON(NgayLapHoaDon,TongTien,MaKhachHang) VALUES(@NgayLap,@TongTien,@MaKH)";

                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);

                OleDbParameter thamSo;

                thamSo = new OleDbParameter("@NgayLap", OleDbType.Date);
                thamSo.Value = ngaylap.ToString();
                lenh.Parameters.Add(thamSo);

                thamSo = new OleDbParameter("@TongTien", OleDbType.Double);
                thamSo.Value = tongtien;
                lenh.Parameters.Add(thamSo);

                thamSo = new OleDbParameter("@MaKH", OleDbType.Integer);
                thamSo.Value = maKH;
                lenh.Parameters.Add(thamSo);

                lenh.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
        }
        public static long layMaHDTheoMaKHvaNgayLap (string maKH, DateTime ngaylap)
        {
            OleDbConnection ketNoi = null;
            long maHoaDon = 0;
            try
            {
                ketNoi = KetNoi();
                string chuoiLenh = "SELECT MaHoaDon FROM HOADON WHERE MaKhachHang=@MaKH and NgayLapHoaDon = @NgayLap";
                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);

                OleDbParameter thamSo2 = new OleDbParameter("@MaKH", OleDbType.Char);
                thamSo2.Value = maKH;

                lenh.Parameters.Add(thamSo2);

                thamSo2 = new OleDbParameter("@NgayLap", OleDbType.Date);
                thamSo2.Value = ngaylap.ToString();
                lenh.Parameters.Add(thamSo2);

                OleDbDataReader boLoc = lenh.ExecuteReader();

                if (boLoc.Read())
                {
                    maHoaDon = boLoc.GetInt32(0);

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
            return maHoaDon;
        }
        public static List<HoaDonDTO> layDSHoaDonTheoThangvaNam(string thang, string nam)
        {
            OleDbConnection ketNoi = null;
            List<HoaDonDTO> dsHoaDon = new List<HoaDonDTO>();
            string dauthang = thang + "/1/"+nam;
            string cuoithang = thang +"/31/"+nam;
            try
            {
                ketNoi = AbtractDAO.KetNoi();
                string chuoiLenh = "SELECT * FROM HoaDon where NgayLapHoaDon between '"+dauthang+"' and '"+cuoithang+"'";
                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);

                OleDbDataReader boLoc = lenh.ExecuteReader();

                while (boLoc.Read())
                {
                    HoaDonDTO HoaDon = new HoaDonDTO();
                    HoaDon.MaHoaDon = boLoc.GetInt32(0);
                    if (!boLoc.IsDBNull(1))
                    {
                        HoaDon.NgayLapHoaDon = boLoc.GetDateTime(1);
                    }

                    if (!boLoc.IsDBNull(2))
                    {
                        HoaDon.TongTien = boLoc.GetInt32(2);
                    }
                    dsHoaDon.Add(HoaDon);
                }
            }
            catch (Exception ex)
            {
                dsHoaDon = new List<HoaDonDTO>();
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
            return dsHoaDon;
        }
    }
}
